package com.ideal.manage.esms.controller.alarm;

import com.ideal.manage.esms.bean.DTO.PageDto;
import com.ideal.manage.esms.bean.DTO.Result;
import com.ideal.manage.esms.bean.alarm.Alarm;
import com.ideal.manage.esms.bean.alarm.AlarmHistory;
import com.ideal.manage.esms.bean.project.AlarmInfo;
import com.ideal.manage.esms.bean.sys.User;
import com.ideal.manage.esms.service.alarm.AlarmHistoryService;
import com.ideal.manage.esms.service.alarm.AlarmService;
import com.ideal.manage.esms.service.project.AlarmInfoService;
import com.ideal.manage.esms.service.project.EquipmentManageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author Allen
 */
@Controller
@RequestMapping("alarm")
public class AlarmController {
    private Logger logger = LoggerFactory.getLogger(AlarmController.class);
    @Resource
    private AlarmService alarmService;
    @Resource
    private AlarmHistoryService alarmHistoryService;
    @Resource
    private AlarmInfoService alarmInfoService;
    @Resource
    private EquipmentManageService equipmentManageService;

    public String DateFormat(Date T) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:MM:ss");
        String formatDate = simpleDateFormat.format(T);
        return formatDate;
    }

    /**
     * 报警列表请求
     *
     * @param model
     */
    @RequestMapping("alarm_list")
    public void alarmList(Model model) {
        //获取报警信息
        alarmService.getAlarmInfo();

    }

    /**
     * 警告列表请求
     *
     * @param pageNum
     * @param request
     * @return
     */
    @RequestMapping("alarm_data")
    @ResponseBody
    public PageDto alarmData(int pageNum, HttpServletRequest request) {
        PageDto pageDto = alarmService.findAll(pageNum, request);
        return pageDto;
    }

    /**
     * 警告按钮确认状态
     *
     * @param model
     * @param id
     */
    @RequestMapping("operatorAffirm_detail")
    public void operatorDetail(Model model,
                               @RequestParam(value = "id", required = false) String id, int state) {
        Date date = new Date();
        String formatDate = DateFormat(date);
        //输出日志
        logger.info("数据确认时间:" + formatDate);
        alarmHistoryService.saveAffirm(id, date);
        //将获取的状态值放回到页面
        model.addAttribute("state", state);
        model.addAttribute("id", id);
        //通过传回的id查找数据放回页面
        //将创建人回显页面
        List<AlarmInfo> allAlarmInfos = alarmInfoService.findAllAlarmInfos();
        User user = null;
        for (AlarmInfo aInfo : allAlarmInfos) {
            user = aInfo.getCreateUser();
        }
        model.addAttribute("user", user);
        Alarm alramAllInfo = alarmService.findAlarmById(id);
        model.addAttribute("alramAllInfo", alramAllInfo);

    }

    /**
     * 改变成作废按钮
     *
     * @param id
     */
    @RequestMapping("alter_state")
    @ResponseBody
    public void operateState(String id) {
        //记录当前时间
        Date date = new Date();
        String formatDate = DateFormat(date);
        //输出日志
        logger.info("数据报警作废时间:" + formatDate);
        //入库报警历史表
        alarmHistoryService.saveDestroy(id, date);
        alarmService.updateState(id);
    }

    /**
     * 改变成处理按钮
     *
     * @param id
     */
    @RequestMapping("alter_statecl")
    @ResponseBody
    public void operatorState(String id) {
        //记录当前时间
        Date date = new Date();
        String formatDate = DateFormat(date);
        //输出日志
        logger.info("数据报警处理时间:" + formatDate);
        alarmHistoryService.saveDispose(id, date);
        alarmService.updateStateCl(id);
    }

    /**
     * 报警管理备注信息保存
     *
     * @param id
     * @param operateContent
     * @return
     */
    @RequestMapping("alarm_save")
    @ResponseBody
    public Result alarmSave(String id, String operateContent) {
        Result result = new Result();
        alarmService.save(id, operateContent);
        alarmHistoryService.saveOperateContent(id, operateContent);
        result.setType("alert");
        result.setMessage("保存操作详情成功");
        return result;
    }

    /**
     * 事件记录详细
     *
     * @param id
     * @param model
     */
    @RequestMapping("event_detail")
    public void eventDetail(Model model,
                            @RequestParam(value = "id", required = false) String id) {
        //将时间传入前台
        Alarm alramAllInfo = alarmService.findAlarmById(id);
        model.addAttribute("alramAllInfo", alramAllInfo);

    }

    /**
     * 警告历史记录
     *
     * @param model
     * @param id
     */
    @RequestMapping("history_detail")
    public void historyDetail(Model model,
                              @RequestParam(value = "id", required = false) String id) {
        //将创建人回显页面
        List<AlarmInfo> allAlarmInfos = alarmInfoService.findAllAlarmInfos();
        User user = null;
        for (AlarmInfo aInfo : allAlarmInfos) {
            user = aInfo.getCreateUser();
        }
        model.addAttribute("user", user);
        AlarmHistory alarmHistoryData = alarmHistoryService.findByIdData(id);
        model.addAttribute("alarmHistoryData", alarmHistoryData);

    }
    /****************************筛选按钮请求********************************/
    /**
     * 按钮筛选
     *
     * @return
     */
    @RequestMapping("condition_query")
    @ResponseBody
    public PageDto alarmData(HttpServletRequest request, @RequestParam("condition") List<String> condition, int pageNum) {
        PageDto alarmToday = null;
        try {
            alarmToday = alarmService.findByToday(request, condition, pageNum);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return alarmToday;
    }

    /**
     * 报警soc
     * @param id
     * @param time
     * @return
     */
    @RequestMapping("alram_soc")
    @ResponseBody
    public Map<String, Object> alramSoc(String id, String time){
        try {
            return alarmService.getAlarmSoc(id ,time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 报警总电压
     * @param id
     * @param time
     * @return
     */
    @RequestMapping("alram_battery")
    @ResponseBody
    public Map<String, Object> alramBattery(String id, String time){
        try {
            return alarmService.getAlarmBattery(id ,time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 报警总电压
     * @param id
     * @param time
     * @return
     */
    @RequestMapping("alram_electricity")
    @ResponseBody
    public Map<String, Object> alramElectricity(String id, String time){
        try {
            return alarmService.getAlarmElectricity(id ,time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
